<script setup>
import "https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.1.0-beta.0/libs/cn/index.js";
import "@/utils/chatBot.js";
import { userInfoStore } from "@/store/userInfoStore.js";
import {
  Management,
  Promotion,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom,
  ChatLineRound,
  View, Shop, Monitor, Medal, IceCreamRound, Apple, Service, Setting,MapLocation ,
  Document ,
  Ship,
  Compass
} from "@element-plus/icons-vue";
import avatar from "@/assets/default.png";
import { ElMessage, ElMessageBox } from "element-plus";
import { userTokenStore } from "@/store/userTokenStore.js";
import Router from "@/router/index.js";
import {onMounted} from "vue";
import {AccessEnum} from "@/access/accessEnums.js";


const InfoStore = userInfoStore();
const TokenStore = userTokenStore();
// const GetInfoForUser = async () => {
//   const result = userInfoStore().info;
//   console.log("user: ", result.data);
//   InfoStore.setInfo(result.data);
// };
// GetInfoForUser();
const handlerForUser = (command) => {
  if (command === "logout") {
    ElMessageBox.confirm("是否确认退出登录", "温馨提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        InfoStore.removeInfo();
        TokenStore.removeToken();
        ElMessage({
          type: "success",
          message: "退出登录成功！",
        });
        await Router.push("/login");
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消退出登录!",
        });
      });
  } else {
    Router.push("/mainPage/user/" + command);
  }
};

//
onMounted(() => {
  // 3 秒后重定向到登录页面
  if(userInfoStore().info)
  ElMessage.success("欢迎回来，" + InfoStore.info.username);
});
</script>
<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu
        active-text-color="#ffd04b"
        background-color="#232323"
        text-color="#fff"
        router
        :unique-opened="false"
        :default-openeds="['personal-center']"
      >
        <el-menu-item index="/mainPage/article/Category">
          <el-icon>
            <Management />
          </el-icon>
          <span>笔记分类</span>
        </el-menu-item>
        <el-menu-item index="/mainPage/article/Manage">
          <el-icon>
            <Promotion />
          </el-icon>
          <span>笔记管理</span>
        </el-menu-item>
        <el-menu-item index="/mainPage/app/home">
          <el-icon><Service /></el-icon>
          <span>App主页</span>
        </el-menu-item>
        <el-sub-menu  index="graph-center">
          <template #title>
            <el-icon>
              <Compass />
            </el-icon>
            <span>格智学舟</span>
          </template>
          <el-menu-item index="/mainPage/user/Graph">
            <el-icon style="color: white;"><map-location /></el-icon>
            <span>知识地图</span>
          </el-menu-item>
          <el-menu-item index="/mainPage/ai/userFileList">
            <el-icon><Document /></el-icon>
            <span>我的文件</span>
          </el-menu-item>
          <el-menu-item index="/mainPage/ai/chat">
            <el-icon><ChatLineRound /></el-icon>
            <span>聊天界面</span>
          </el-menu-item>
        </el-sub-menu>
      
        <el-sub-menu  index="personal-center">
          <template #title>
            <el-icon>
              <UserFilled />
            </el-icon>
            <span>个人中心</span>
            <span v-if="userInfoStore().info.user_role === AccessEnum.ADMIN">&nbsp;(管理员)</span>
          </template>
          <el-menu-item index="/mainPage/user/Info">
            <el-icon>
              <User />
            </el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item index="/mainPage/user/ResetPsd">
            <el-icon>
              <EditPen />
            </el-icon>
            <span>重置密码</span>
          </el-menu-item>
          <el-menu-item index="/mainPage/user/app">
            <el-icon><Setting /></el-icon>
            <span>我的应用</span>
          </el-menu-item>
            <el-sub-menu v-if="userInfoStore().info.user_role === AccessEnum.ADMIN">
              <template #title>
                <el-icon>
                  <Shop />
                </el-icon>
                <span>管理中心</span>
              </template>
              <el-menu-item index="/admin/user">
                <el-icon><User /></el-icon>
                <span>用户管理</span>
              </el-menu-item>
              <el-menu-item index="/admin/app">
                <el-icon><Monitor /></el-icon>
                <span>应用管理</span>
              </el-menu-item>
              <el-menu-item index="/admin/question">
                <el-icon><Apple /></el-icon>
                <span>题目管理</span>
              </el-menu-item>
              <el-menu-item index="/admin/review">
                <el-icon><Medal /></el-icon>
                <span>评分管理</span>
              </el-menu-item>
              <el-menu-item index="/admin/userAnswer">
                <el-icon><IceCreamRound /></el-icon>
                <span>回答管理</span>
              </el-menu-item>
            </el-sub-menu>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <!-- 右侧主区域 -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>
          尊敬的&nbsp<strong
            style="color: #3498db; text-shadow: 1px 1px 1px #999"
          >
            {{
              InfoStore.info.nickname
                ? InfoStore.info.nickname
                : InfoStore.info.username
            }} </strong
          >&nbsp同学，欢迎来到格智学舟平台，期待您在笔记管理和知识图谱中收获便捷与有序。
          点击右下角有惊喜！
        </div>
        <el-dropdown placement="bottom-end" @command="handlerForUser">
          <span class="el-dropdown__box">
            <el-avatar
              :src="InfoStore.info.userPic ? InfoStore.info.userPic : avatar"
            />
            <el-icon>
              <CaretBottom />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="Info" :icon="User"
                >基本资料</el-dropdown-item
              >
              <el-dropdown-item command="ResetPsd" :icon="EditPen"
                >重置密码</el-dropdown-item
              >
              <el-dropdown-item command="logout" :icon="SwitchButton"
                >退出登录</el-dropdown-item
              >
              <el-dropdown-item command="Chat">
                <el-icon><chat-line-round /></el-icon>
                开始对话
              </el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <!-- 中间区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
      <!-- 底部区域 -->
      <el-footer>格智学舟 欢迎你</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;

  .el-aside {
    background-color: #232323;

    &__logo {
      height: 120px;
      background: url("@/assets/logo1.jpg") no-repeat center / 100px auto;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown__box {
      display: flex;
      align-items: center;

      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }

  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>
